package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.BathroomDetailActivity;
import com.wnhz.work.yz.activity.PlayVideoActivity;
import com.wnhz.work.yz.activity.ShoppingDetailsActivity;
import com.wnhz.work.yz.activity.WenKuHtmlActivity;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.SearchBean;

import org.xutils.x;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017/3/1.
 */
public class SearchAdapter extends BaseRVAdapter {
    private List<SearchBean.InfoEntity> infos = new ArrayList<>();
    public static final int TYPE0 = 1230;
    public static final int TYPE1 = 1231;
    public static final int TYPE2 = 1232;
    public static final int TYPE3 = 1233;
    public static final int TYPE4 = 1234;
    List<SearchBean.InfoEntity.DataListEntity> keyF0 = new ArrayList<>();
    List<SearchBean.InfoEntity.DataListEntity> keyF1 = new ArrayList<>();
    List<SearchBean.InfoEntity.DataListEntity> keyF2 = new ArrayList<>();
    List<SearchBean.InfoEntity.DataListEntity> keyF3 = new ArrayList<>();
    List<SearchBean.InfoEntity.DataListEntity> keyF4 = new ArrayList<>();
    List<SearchBean.InfoEntity.DataListEntity> key0 = new ArrayList<>();
    List<SearchBean.InfoEntity.DataListEntity> key1 = new ArrayList<>();
    List<SearchBean.InfoEntity.DataListEntity> key2 = new ArrayList<>();
    List<SearchBean.InfoEntity.DataListEntity> key3 = new ArrayList<>();
    List<SearchBean.InfoEntity.DataListEntity> key4 = new ArrayList<>();
    private int num = 5;
    private int page0, page1, page2, page3, page4;
    private Adapter0 adapter0;
    private Adapter1 adapter1;
    private Adapter2 adapter2;
    private Adapter3 adapter3;
    private Adapter4 adapter4;
    private boolean flag0, flag1, flag2, flag3, flag4;

    /**
     * 构造器
     *
     * @param context 上下文
     * @param infos   数据集合
     */
    public SearchAdapter(Context context, List<SearchBean.InfoEntity> infos, RecyclerView recyclerView) {
        super(context, infos);
        this.infos = infos;
        this.mContext = context;
    }


    private int currentType = 1230;


    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_search_parent;
    }


    @Override
    public void onBind(final BaseViewHolder holder, final int position) {
        String name = infos.get(position).getClassName();
//
        if (name != null) {
            if (name.contains("家居"))
                currentType = TYPE0;
            else if (name.contains("浴室"))
                currentType = TYPE1;
            else if (name.contains("内装"))
                currentType = TYPE2;
            else if (name.contains("文库"))
                currentType = TYPE3;
            else if (name.contains("视频"))
                currentType = TYPE4;
        }
        RecyclerView recycler = holder.getView(R.id.recycler2);
        recycler.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        holder.setTextView(R.id.title, infos.get(position).getClassName());
        switch (currentType) {
            case TYPE0:
                keyF0 = (List<SearchBean.InfoEntity.DataListEntity>) infos.get(position).getDataList();
                if (!flag0) {
                    for (int i = 0; i < num; i++) {
                        if (key0.size() < keyF0.size()) {
                            //   Log.e("--0", key0.size() + "--" + key.size() + "--" + i + num * page0);

                            key0.add(keyF0.get(i + num * page0));
                            holder.getView(R.id.more).setVisibility(View.VISIBLE);
                        } else {
                            holder.getView(R.id.more).setVisibility(View.GONE);
                        }
                    }
                    flag0 = true;
                }
                holder.getView(R.id.more).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        page0++;
                        for (int i = 0; i < num; i++) {
                            Log.e("--0", key0.size() + "--" + keyF0.size() + "--" + i + num * page0);
                            if (key0.size() < keyF0.size()) {
//                                if (key0.size() >= 10) {
//                                    key0.remove(0);
//                                    Log.e("--", "----");
//                                }
                                key0.add(keyF0.get(i + num * page0));
                                holder.getView(R.id.more).setVisibility(View.VISIBLE);
                            } else {
                                holder.getView(R.id.more).setVisibility(View.GONE);
                            }
                        }
                        adapter0.notifyDataSetChanged();
                    }
                });
                adapter0 = new Adapter0(mContext, key0);
                recycler.setAdapter(adapter0);

                break;
            case TYPE1:
                // holder.setTextView(R.id.title, "整体卫浴");
                keyF1 = (List<SearchBean.InfoEntity.DataListEntity>) infos.get(position).getDataList();
                if (!flag1) {
                    for (int i = 0; i < num; i++) {
                        Log.e("--1", key1.size() + "--" + keyF1.size() + "--" + (i + num * page1));
                        if (key1.size() < keyF1.size()) {
                            key1.add(keyF1.get(i + num * page1));
                            holder.getView(R.id.more).setVisibility(View.VISIBLE);
                        } else {
                            holder.getView(R.id.more).setVisibility(View.GONE);
                        }
                    }
                    flag1 = true;
                }
                holder.getView(R.id.more).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        page1++;
                        for (int i = 0; i < num; i++) {
                            Log.e("--1", key1.size() + "--" + keyF1.size() + "--" + (i + num * page1));
                            if (key1.size() < keyF1.size()) {
                                key1.add(keyF1.get(i + num * page1));
                                holder.getView(R.id.more).setVisibility(View.VISIBLE);
                            } else {
                                holder.getView(R.id.more).setVisibility(View.GONE);
                            }
                        }
                        adapter1.notifyDataSetChanged();
                    }
                });
                adapter1 = new Adapter1(mContext, key1);
                recycler.setAdapter(adapter1);
                break;
            case TYPE2:
                //  if (name.equals("工业化内装"))
                keyF2 = (List<SearchBean.InfoEntity.DataListEntity>) infos.get(position).getDataList();
                if (!flag2) {
                    for (int i = 0; i < num; i++) {
                        if (key2.size() < keyF2.size()) {
                            //    Log.e("--2", key2.size() + "--" + key.size() + "--" + i + num * page2);
                            key2.add(keyF2.get(i + num * page2));
                            holder.getView(R.id.more).setVisibility(View.VISIBLE);
                        } else {
                            holder.getView(R.id.more).setVisibility(View.GONE);
                        }
                    }
                    flag2 = true;
                }
                holder.getView(R.id.more).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        page2++;
                        for (int i = 0; i < num; i++) {
                            if (key2.size() < keyF2.size()) {
                                //     Log.e("--2", key2.size() + "--" + key.size() + "--" + i + num * page2);
                                key2.add(keyF2.get(i + num * page2));
                                holder.getView(R.id.more).setVisibility(View.VISIBLE);
                            } else {
                                holder.getView(R.id.more).setVisibility(View.GONE);
                            }
                        }
                        adapter2.notifyDataSetChanged();

                    }
                });
                adapter2 = new Adapter2(mContext, key2);
                recycler.setAdapter(adapter1);
                break;
            case TYPE3:
                //if (name.equals("文库"))
                keyF3 = (List<SearchBean.InfoEntity.DataListEntity>) infos.get(position).getDataList();
                if (!flag3) {
                    for (int i = 0; i < num; i++) {
                        if (key3.size() < keyF3.size()) {
                            //Log.e("--3", key3.size() + "--" + key.size() + "--" + i + num * page3);
                            key3.add(keyF3.get(i + num * page3));
                            holder.getView(R.id.more).setVisibility(View.VISIBLE);
                        } else {
                            holder.getView(R.id.more).setVisibility(View.GONE);
                        }
                    }
                    flag3 = true;
                }
                holder.getView(R.id.more).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        page3++;
                        for (int i = 0; i < num; i++) {
                            if (key3.size() < keyF3.size()) {
                                //     Log.e("--3", key3.size() + "--" + key.size() + "--" + i + num * page3);
                                key3.add(keyF3.get(i + num * page3));
                                holder.getView(R.id.more).setVisibility(View.VISIBLE);
                            } else {
                                holder.getView(R.id.more).setVisibility(View.GONE);
                            }
                        }
                        adapter3.notifyDataSetChanged();

                    }
                });
                adapter3 = new Adapter3(mContext, key3);
                recycler.setAdapter(adapter3);
                break;
            case TYPE4:
                // (name.equals("视频"))
                keyF4 = (List<SearchBean.InfoEntity.DataListEntity>) infos.get(position).getDataList();
                if (!flag4) {
                    for (int i = 0; i < num; i++) {
                        if (key4.size() < keyF4.size()) {
                            // Log.e("--4", key4.size() + "--" + key.size() + "--" + i + num * page4);
                            key4.add(keyF4.get(i + num * page4));
                            holder.getView(R.id.more).setVisibility(View.VISIBLE);
                        } else {
                            holder.getView(R.id.more).setVisibility(View.GONE);
                        }
                    }
                    flag4 = true;
                }
                holder.getView(R.id.more).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        page4++;
                        for (int i = 0; i < num; i++) {
                            if (key4.size() < keyF4.size()) {
                                //    Log.e("--4", key4.size() + "--" + key.size() + "--" + i + num * page4);
                                holder.getView(R.id.more).setVisibility(View.VISIBLE);
                                key4.add(keyF4.get(i + num * page4));
                            } else {
                                holder.getView(R.id.more).setVisibility(View.GONE);
                            }
                        }
                        adapter4.notifyDataSetChanged();

                    }
                });


                adapter4 = new Adapter4(mContext, key4);
                recycler.setAdapter(adapter4);
                break;
        }


    }


    class Adapter0 extends BaseRVAdapter {

        private List<SearchBean.InfoEntity.DataListEntity> key;

        public Adapter0(Context context, @NonNull List list) {
            super(context, list);
            key = list;
        }

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.bath_item;
        }

        @Override
        public void onBind(final BaseViewHolder holder2, final int position) {
            holder2.setTextView(R.id.tv_name, key.get(position).getName());
            DecimalFormat df = new DecimalFormat("######0.00");
            /*try {
                holder2.setTextView(R.id.tv_price, df.format(Double.parseDouble(key.get(position).getPrice()) * Double.parseDouble(MyApplication.getInstance().getUserBean().getRatio().toString())));
            } catch (Exception e) {*/
                holder2.setTextView(R.id.tv_price, df.format(Double.parseDouble(key.get(position).getPrice())));
//            }
          //  holder2.setTextView(R.id.tv_price, key.get(position).getPrice());
            holder2.setTextView(R.id.tv_number, key.get(position).getDescribe());
            //  handler.sendEmptyMessage(0);
            x.image().bind(holder2.getImageView(R.id.iv_img), key.get(position).getLogo_pic());
            holder2.getView(R.id.ll_max0).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ShoppingDetailsActivity.class);
                    intent.putExtra("goodsId", key.get(position).getGoods_id());
                    mContext.startActivity(intent);
                }
            });

        }

    }

    class Adapter1 extends BaseRVAdapter {

        private List<SearchBean.InfoEntity.DataListEntity> key;

        public Adapter1(Context context, @NonNull List list) {
            super(context, list);
            key = list;
        }

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.search_weiyu;
        }

        @Override
        public void onBind(final BaseViewHolder holder2, final int position) {

            holder2.setTextView(R.id.weiyu_tv, key.get(position).getName());
            x.image().bind(holder2.getImageView(R.id.weiyu), key.get(position).getLogo_pic());
            holder2.getView(R.id.ll_max1).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, BathroomDetailActivity.class);
                    intent.putExtra("packageId", key.get(position).getGoods_id());
                    mContext.startActivity(intent);
                }
            });

        }

    }

    class Adapter2 extends BaseRVAdapter {

        private List<SearchBean.InfoEntity.DataListEntity> key;

        public Adapter2(Context context, @NonNull List list) {
            super(context, list);
            key = list;
        }

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.search_weiyu;
        }

        @Override
        public void onBind(final BaseViewHolder holder2, final int position) {
            holder2.setTextView(R.id.weiyu_tv, key.get(position).getName());
            x.image().bind(holder2.getImageView(R.id.weiyu), key.get(position).getLogo_pic());
        }

    }

    class Adapter3 extends BaseRVAdapter {

        private List<SearchBean.InfoEntity.DataListEntity> key;

        public Adapter3(Context context, @NonNull List list) {
            super(context, list);
            key = list;
        }

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.item_library;
        }

        @Override
        public void onBind(final BaseViewHolder holder2, final int position) {
            holder2.setTextView(R.id.title_tv, key.get(position).getName());
            holder2.setTextView(R.id.tv_time, key.get(position).getCreate_at());
            holder2.setTextView(R.id.tv_numbers, key.get(position).getReadnums());
            x.image().bind(holder2.getImageView(R.id.img), key.get(position).getLogo_pic());
            holder2.getView(R.id.line).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, WenKuHtmlActivity.class);
                    intent.putExtra("html", key.get(position).getUrl());
                    mContext.startActivity(intent);
                }
            });
        }

    }

    class Adapter4 extends BaseRVAdapter {

        private List<SearchBean.InfoEntity.DataListEntity> key;

        public Adapter4(Context context, @NonNull List list) {
            super(context, list);
            key = list;
        }

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.video_item;
        }

        @Override
        public void onBind(final BaseViewHolder holder2, final int position) {
            holder2.setTextView(R.id.tv_title, key.get(position).getName());
            holder2.setTextView(R.id.tv_time, key.get(position).getDescribe());
            x.image().bind(holder2.getImageView(R.id.iv_img), key.get(position).getLogo_pic());
            holder2.getView(R.id.ll_max4).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, PlayVideoActivity.class);
                    intent.putExtra("img",key.get(position).getLogo_pic());
                    intent.putExtra("videoUrl", key.get(position).getUrl());
                    intent.putExtra("title", key.get(position).getName());
                    intent.putExtra("des", key.get(position).getDescribe());
                    mContext.startActivity(intent);
                }
            });
        }

    }
}
